/*
 * Records package
 */
package edu.gatech.oad.records;

//import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import edu.gatech.oad.user.Patient;
import edu.gatech.oad.user.User;
import edu.gatech.oad.user.Receptionist;

/**
 * Panel that represents a labs results
 * @author Nathan Hurley
 * @version 1.0
 */
public class LabResultsPanel extends JPanel {
	
	/**
	 * Serializable
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The current LabResults
	 */
	private final LabResults curResults;
	
	/**
	 * The current user of the panel
	 */
	private final User curUser;
	
	/**
	 * Text area for results
	 */
	private final JTextArea results;
	
	/**
	 * Button for updating the results
	 */
	private final JButton updateLabResults;

	/**
	 * Constructs a LabResultsPanel to display the labs results
	 * @param curResults the labs results
	 * @param curUser the current user
	 */
	public LabResultsPanel(LabResults curResults, User curUser){ // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		this.curResults = curResults;
		this.curUser = curUser;
		updateLabResults = new JButton("Save Changes");
		results = new JTextArea(this.curResults.getResults());
		results.getDocument().addDocumentListener(new TextChangedListener());
		preparePanel();
		changeAccess();
		//setPreferredSize(new Dimension(500, 500));
	}
	
	/**
	 * Determines if it should display a TextArea or a JLabel
	 */
	public final void changeAccess(){
		if(!(this.curUser instanceof Patient || this.curUser instanceof Receptionist)){
			add(new JScrollPane(results));
			add(updateLabResults);//add if user isnt a patient
		}
		else{
			add(new JLabel("<html>" + this.curResults.getResults() + "</html>"));
		}
	}
	
	/**
	 * Sets the layout and adds action listeners for the components
	 */
	public final void preparePanel(){
		setBorder(new TitledBorder(this.curResults.getOrder().getName()));
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		updateLabResults.addActionListener(new ButtonListener());
		updateLabResults.setAlignmentX(CENTER_ALIGNMENT);
		results.setLineWrap(true);
		results.setWrapStyleWord(true);
	}
	
	/**
	 * Button Listener for the updateLabResults button
	 * @author Nathan Hurley
	 *
	 */
	private class ButtonListener implements ActionListener{
		/**
		 * Sets the labs results given the text in the TextArea
		 * @param e the action that has occurred
		 */
		public void actionPerformed(ActionEvent e){
			curResults.setResults(results.getText());
		}
		
		/**
		 * @return String indicating ButtonListener is for the LabResults class
		 */
		public String toString(){
			return "ButtonListener for the LabResults class";
		}
	}
	
	/**
	 * Updates the results JTextArea if any changes are made to it
	 * @author Nathan Hurley
	 */
	private class TextChangedListener implements DocumentListener{
		/**
		 * Empty method for changed
		 * @param e the event that has occurred
		 */
		public void changedUpdate(DocumentEvent e){ // $codepro.audit.disable emptyMethod
		}
		
		/**
		 * If text is deleted results are saved
		 * @param e the event that has occurred
		 */
		public void removeUpdate(DocumentEvent e){
			curResults.setResults(results.getText());
		}
		
		/**
		 * Saves results if text has been added
		 * @param e the event that has occurred
		 */
		public void insertUpdate(DocumentEvent e){
			curResults.setResults(results.getText());
		}
		
		/**
		 * @return location of this listener
		 */
		public String toString(){
			return "Text change listener for the LabResultsPanel";
		}
	}
	
	/**
	 * @return the panels corresponding LabResults
	 */
	public LabResults getResults(){
		return curResults;
	}
	
	/**
	 * @param o the object to compare this to
	 * @return true if parameter and this have the same LabResults
	 */
	public boolean equals(Object o){
		if(this == o){
			return true;
		}
		if(o instanceof LabResultsPanel){
			final LabResultsPanel l = (LabResultsPanel) o;
			if(l.getResults().equals(this.getResults())){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * @return the hashcode for the LabResults
	 */
	public int hashCode(){
		return getResults().hashCode();
	}
	
}
